package com.codename1.samples;


import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.ui.Container;
import com.codename1.ui.plaf.CSSBorder;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class CSSBorderTest {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Hi World", BoxLayout.y());
        Label lineTest = new Label("1px solid #ff0000");
        lineTest.getStyle().setBorder(
                new CSSBorder()
                        .backgroundColor("#00ff00")
                        .borderColor("#ff0000")
                        .borderWidth("1pt")
                        .borderStyle("solid")
                        .borderRadius("2mm")
                
        );
        hi.add(lineTest);
        
        lineTest = new Label("1px solid #ff0000");
        lineTest.getStyle().setBorder(
                new CSSBorder()
                        .backgroundColor("#00ff00")
                        .borderColor("#ff0000")
                        .borderWidth("1pt 5pt 1px 5pt")
                        .borderStyle("solid")
                        .borderRadius("2mm")
                
        );
        hi.add(lineTest);
        
        lineTest = new Label("1px solid #ff0000");
        lineTest.getStyle().setBorder(
                new CSSBorder()
                        .backgroundColor("#00ff00")
                        .borderColor("#ff0000")
                        .borderWidth("1pt")
                        .borderStyle("dashed")
                        .borderRadius("2mm")
                
        );
        hi.add(lineTest);
        
        lineTest = new Label("1px solid #ff0000");
        CSSBorder border = new CSSBorder()
                        .backgroundColor("#00ff00")
                        .borderColor("#ff0000")
                        .borderWidth("1pt")
                        .borderStyle("dashed")
                        .borderRadius("2mm");
        border = new CSSBorder(border.toCSSString());
        lineTest.getStyle().setBorder(border);
                
                
        
        hi.add(lineTest);
        
        Container cnt = new Container();
        cnt.getStyle().setBorder(new CSSBorder(theme).borderImage(theme.getImage("dashbg_landscape.png"), 0.4));
        cnt.add(new Label("Setting CSS 9-Piece border manually"));
        hi.add(cnt);
        
        
        cnt = new Container();
        cnt.setUIID("NinePiece");
        cnt.add(new Label("9-piece border with UIID"));
        hi.add(cnt);
        
        cnt = new Container();
        cnt.setUIID("BorderRadiusTopLeft");
        cnt.add(new Label("Border Radius Top Left"));
        hi.add(cnt);
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}
